<?php
/*
 * 类别函数
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 68b96d2c49819b442e0ccc1b0c77a5ec9002bb85 $
 */

class Category {

	public static function textTree($parentid, $category, $level = 0) {
		$children = array();
		foreach ($category as $catid => $cat) {
			if ($cat['parentid'] == $parentid) {
				$children[$catid] = $cat;
			}
		}
		$tab      = str_repeat('-', $level);
		$levelNew = $level + 1;
		if ($children) {
			foreach ($children as $child) {
				echo $tab . $child['catname'] . PHP_EOL;
				if ($child['child']) {
					$func = __FUNCTION__;
					self::$func($child['catid'], $category, $levelNew);
				}
			}
		}
	}

	public static function getParentsAndSelf($catid, $moduleid) {
		$CATEGORY  = self::module($moduleid);
		$strPids = $CATEGORY[$catid]['arrparentid'];
		$arrPids = explode(',', $strPids);
		$arrPids[] = $catid;
		array_shift($arrPids);
		$cats = array();
		foreach($arrPids as $_cid) {
			$cats[] = $CATEGORY[$_cid];
		}
		return $cats;
	}

	/**
	 * 单线直系亲属, 0, 1, [10]
	 * @param      $mod
	 * @param      $catid
	 * @param bool $self
	 * @return array
	 */
	public static function directRelative($mod, $catid, $self = true) {
		$cats = self::module($mod);
		if ($catid) {
			$directRelative = $cats[$catid]['arrparentid'];
			if ($self) {
				$directRelative .= ','.$catid;
			}
			return explode(',', $directRelative);
		} else {
			return array(0);
		}

	}

	/**
	 * 自己以及父级的所有ID, 木有 0
	 * @param $mod
	 * @param $catid
	 * @return array
	 */
	public static function directSeries($mod, $catid){
		$cats = self::module($mod);
		$catids = self::directRelative($mod, $catid, true);
		array_shift($catids);
		$return = array();
		if ($catids) {
			foreach($catids as $_catid) {
				$return[$_catid] = $cats[$_catid];
			}
		}
		return $return;
	}

	/**
	 * 亲朋好友, 群组集合
	 * @param $mod
	 * @param $catid
	 * @return array
	 */
	public static function relatives($mod, $catid) {
		$directRelatives = self::directRelative($mod,$catid);
		$return  = array();
		foreach($directRelatives as $relative) {
			$return[$relative] = self::children($mod, $relative);
		}
		return $return;
	}

	/**
	 * 所有孩子
	 * @param $mod
	 * @param $catid
	 * @return array
	 */
	public static function children($mod, $catid) {
		$cats = self::module($mod);
		$return = array();
		foreach($cats as $_catid => $_cat) {
			if ($_cat['parentid'] == $catid) {
				$return[$_catid] = $_cat;
			}
		}
		return $return;
	}

	/**
	 * 同级元素
	 * @param $mod
	 * @param $catid
	 * @return array
	 */
	public static function siblings($mod, $catid){
		$cats = self::module($mod);
		$parentid = $cats[$catid]['parentid'];
		$return = array();
		foreach($cats as $_catid => $_cat) {
			if ($_cat['parentid'] == $parentid) {
				$return[$_catid] = $_cat;
			}
		}
		return $return;
	}

	/**
	 * 获取模块的分类
	 * @param        $mod
	 * @param string $catid
	 * @return array|mixed|string
	 */
	public static function module($mod, $catid = '') {
		$moduleid = Module::info($mod, 'moduleid');
		$cats = PCache::read('category-'.$moduleid.'.php');
		return $catid ? (isset($cats[$catid]) ? $cats[$catid] : '') : $cats;
	}

}